﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using MobileWeb.Models;

namespace MobileWeb.Helpers
{
    public class SearchHelper
    {
        public static List<ApartmentBasic> GetApartments(string floorId, string numOfRoomsId, string areaId, string priceId)
        {
            var xmlContent = XmlHelper.GetXmlContent(String.Format("http://www.centar-bundek.hr/xml/search/{0}/{1}/{2}/{3}", floorId, numOfRoomsId, areaId, priceId));

            var elementList = xmlContent.Elements();
            var apartments = new List<ApartmentBasic>();
            foreach (var item in elementList)
            {
                var price = Double.Parse(item.Element("stateprice").Value, CultureInfo.CurrentCulture);
                if(price != 0)
                {
                    var area = Double.Parse(item.Element("area").Value, CultureInfo.CreateSpecificCulture("en-US"));
                    var totalPrice = area * price;

                    var apartment = new ApartmentBasic()
                    {
                        Id = Convert.ToInt32(item.Element("id").Value),
                        Area = area,
                        Floor = item.Element("floor").Value,
                        NumOfRooms = item.Element("roomsnumber").Value,
                        Price = price,
                        TotalPrice = totalPrice
                    };
                    apartments.Add(apartment);
                }
            }
            return apartments;
        }

        public static ApartmentDetails GetApartment(int id)
        {
            var apartmentXml = XmlHelper.GetXmlContent(String.Format("http://www.centar-bundek.hr/xml/apartment/{0}", id));

            #region code processing
            
            var code = apartmentXml.Element("code").Value;
            var buildingName = code.Remove(code.IndexOf('-'));

            var apartmentNum = String.Empty;
            var counter = 0;
            foreach (var letter in code)
            {
                if (letter == '.')
                {
                    counter++;
                }
                else if (counter == 2)
                {
                    apartmentNum += letter;
                }
            }

            code = buildingName + "-" + apartmentNum;
            var pdfCode = buildingName + "_" + apartmentNum + ".pdf";

            #endregion
            
            var floor = apartmentXml.Element("floor").Value;
            var numOfRooms = apartmentXml.Element("roomsnumber").Value;
            var area = apartmentXml.Element("area").Value;

            var apartment = new ApartmentDetails()
            {
                TitleCode1 = "STAN " + code + " - " + floor + " - " + numOfRooms + " - " + area + " m2",
                TitleCode2 = apartmentXml.Element("code").Value + " - " + floor + " - " + "DILATACIJA " + buildingName,
                State = apartmentXml.Element("state").Value,
                Area = Double.Parse(apartmentXml.Element("area").Value, CultureInfo.CurrentCulture),
                Floor = floor,
                NumOfRooms = numOfRooms,
                PhotoUrl = apartmentXml.Element("graphics").Element("flrlayout").Value,
                Rooms = new List<Room>(),
                PdfUrlCode = pdfCode
            };

            var roomsXml = apartmentXml.Elements("rooms").ElementAt(0).Elements();
            foreach (var xElement in roomsXml)
            {
                var room = new Room()
                {
                    Mark = xElement.Element("mark").Value,
                    Name = xElement.Element("type").Value,
                    Area = xElement.Element("area").Value
                };
                apartment.Rooms.Add(room);
            }
            
            return apartment;
        }
    }
    
}